/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.corespring.restaurant.web.controller;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.corespring.restaurant.model.Local;
import org.corespring.restaurant.service.LocalManager;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import org.corespring.restaurant.model.Usuario;
import org.corespring.restaurant.service.UsuarioManager;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.validation.BindingResult;
/**
 *
 * @author Aldo
 */

@Controller
public class LocalController {

    LocalManager localManager;
    UsuarioManager usuarioManager;

    @Autowired
    public LocalController(LocalManager oLocalManager, UsuarioManager oUsuarioManager){
        this.localManager=oLocalManager;
        this.usuarioManager = oUsuarioManager;
    }

    @RequestMapping("/adm_locales.htm")
    public void adm_locales(Model model, @ModelAttribute("Local") Local local) {
		List<Local> locales = localManager.obtenerLocales();
                model.addAttribute("locales", locales);
                model.addAttribute("local", local);
    }

    @RequestMapping(value = "/adm_registroLocal.htm", method = RequestMethod.GET)
    public
    @ModelAttribute("local")
    Local obtenerLocal(
            @RequestParam(value = "idlocal", required = false) String idLocal, HttpServletRequest request) {
        if(idLocal ==null){
             return new Local();
        }
        if (!(idLocal.isEmpty() || idLocal.equals(""))) {            
            Local local = localManager.obtenerLocal(new Integer(idLocal));
            if (local == null) {
                return new Local();
            }
            return local;
        }
        return new Local();
    }

    @RequestMapping(value = "/adm_registroLocal.htm", method = RequestMethod.POST)
    public String guardarLocal( @RequestParam(value = "cboEstado", required = false) String estado,
            @ModelAttribute("local") Local local,
            BindingResult result, SessionStatus status) {
        Date fecha = new Date();
        String hora = fecha.getHours()+":"+fecha.getMinutes()+":"+fecha.getSeconds();
        Usuario usu = usuarioManager.obtenerUsuarioById("admin");


        local.setEstado(estado);
        local.setFreg(fecha);
        local.setHreg(hora);
        local.setUsuario(usu);
        localManager.grabarLocal(local);
        return "redirect:adm_locales.htm";
    }

    @RequestMapping("/eliminarLocal.htm")
    public String eliminarLocal(
            @RequestParam(value = "idlocal", required = false) String idLocal) {
        localManager.eliminarLocal(new Integer(idLocal));
        return "redirect:adm_locales.htm";
    }

    @RequestMapping("/adm_verMapa.htm")
    public void adm_verMapa() {
    }

    @RequestMapping("/locales.htm")
    public void locales(Model model, @ModelAttribute("Local") Local local) {
		List<Local> locales = localManager.obtenerLocales();
                model.addAttribute("locales", locales);
                model.addAttribute("local", local);
    }


}
